package com.lancea12.buster.android;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.nio.Buffer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultRedirectHandler;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class LoginNetwork implements Runnable {
	
    private AndroidHttpClient client;
    private String user;
    private String password;
    private Handler mHandler;
    private Activity caller;
    private BasicHttpContext con;
	
	public LoginNetwork(String user, String password, Handler mHandler, Activity caller) {
		this.user = user;
		this.password = password;
		this.mHandler = mHandler;
		this.caller = caller;
	}

    @Override
    public void run(){
    	client = AndroidHttpClient.newInstance(
    			"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11");
		con = new BasicHttpContext();
    	loginRequest("http://192.168.2.2/buster/rest/login/?action=login");
    }
    
    private void loginRequest(String url){
    	HttpGet get = new HttpGet(url);
    	get.setHeader("Accept", "application/xrds+xml");
    	try {
			HttpResponse res = client.execute(get, con);
			DefaultRedirectHandler red = new DefaultRedirectHandler();
			if (red.isRedirectRequested(res, con)) {
		        loginRequest(red.getLocationURI(res, con).toString());
		    }else{
	            //DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	            //DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	            //Document doc = docBuilder.parse (new StringBufferInputStream(tempRes));	
	            //NodeList nl = doc.getElementsByTagName("URI");
	            //String responseStr = nl.item(0).getTextContent();
	            Message mes = new Message();
	            Bundle bun = new Bundle();
	            HttpEntity en = res.getEntity();
	            InputStream in = en.getContent();
	            String resStr = "";
	            if (en.isChunked()){
	            	int len = in.read();
		            while(len > 0){
		            	byte buf[] = new byte[len];
		            	in.read(buf);
		            	resStr += new String(buf);
		            	len = in.read();
		            }
	            }
	            //System.err.println(resStr);
	            bun.putString("response", "done");
	            mes.setData(bun);
				mHandler.sendMessage(mes);
		    }
		} catch (IOException e) {
			e.printStackTrace();
		//} catch (ParserConfigurationException e) {
		//	e.printStackTrace();
		//} catch (SAXException e) {
		//	e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		}finally{
			client.close();
		}
    }

}
